package ${basePackage}.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.wht.pirates.page.Pagination;
import com.wht.pirates.toolkit.PrimaryKeyUtil;

import ${basePackage}.dto.${className}SearchDTO;
import ${basePackage}.mapper.${className}Mapper;
import ${basePackage}.model.${className};
import ${basePackage}.service.${className}Service;

/**
* @Author: smili
*/
@Service
public class ${className}ServiceImpl implements ${className}Service {

    @Autowired
    private ${className}Mapper ${lowerClassName}Mapper;

    @Override
    public boolean add(${className} param) {

        ${className} ${lowerClassName} = new ${className}();

        //TODO 检测需要添加字段检测 eg. gmtCreated  gmtModified
<#list columnInfoList as columnInfo>
     <#if columnInfo.lowerCamelName == pkName>
        ${lowerClassName}.set${columnInfo.upperCamelName}(PrimaryKeyUtil.getUid());
     <#else>
        ${lowerClassName}.set${columnInfo.upperCamelName}(param.get${columnInfo.upperCamelName}());
     </#if>
</#list>
        int count = ${lowerClassName}Mapper.insertSelective(${lowerClassName});
        return count > 0 ? true : false;
    }

    @Override
    public boolean delete(${pkType} ${pkName}) {

        int count = ${lowerClassName}Mapper.deleteByPrimaryKey(${pkName});

        return count > 0 ? true : false;
    }

    @Override
    public boolean update(${className} param) {

        ${className} ${lowerClassName} = new ${className}();

        //TODO 检测需要修改的字段，PS：请删除不是需求的字段字段(安全)
<#list columnInfoList as columnInfo>
        ${lowerClassName}.set${columnInfo.upperCamelName}(param.get${columnInfo.upperCamelName}());
</#list>

        int count = ${lowerClassName}Mapper.updateByPrimaryKeySelective(${lowerClassName});

        return count > 0 ? true : false;
    }

    @Override
    public ${className} search(${pkType} ${pkName}) {

        return ${lowerClassName}Mapper.selectByPrimaryKey(${pkName});
    }

    @Override
    public Pagination<${className}> searchForPage(${className}SearchDTO ${lowerClassName}SearchDTO) {

        ${className} ${lowerClassName} = new ${className}();

        //TODO 设置查询参数

        int count = ${lowerClassName}Mapper.selectCount(${lowerClassName});
        if (count == 0) {
            return null;
        }

        PageHelper.startPage(${lowerClassName}SearchDTO.getPageIndex(), ${lowerClassName}SearchDTO.getPageSize());
        List<${className}> ${lowerClassName}s = ${lowerClassName}Mapper.select(${lowerClassName});

        Pagination<${className}> pagination = new Pagination();
        pagination.setTotalCount(count);
        pagination.setPageIndex(${lowerClassName}SearchDTO.getPageIndex());
        pagination.setPageSize(${lowerClassName}SearchDTO.getPageSize());
        pagination.setResult(${lowerClassName}s);
        return pagination;
    }

}
